#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
using namespace std;
const int N = 210, M = 20010;
int n, m;
int f[N][N];
int t[N];
void flody(int k)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
        }
    }
}
int main()
{
    memset(f, 0x3f, sizeof(f));
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> t[i];
    }
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        f[a][b] = f[b][a] = min(f[a][b], c);
    }
    int q = 0;
    cin >> q;
    int pos = 0;
    while (q--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        while (pos < n && t[pos] <= c)
        {
            flody(pos++);
        }
        if (t[a] > c || t[b] > c || f[a][b] == 0x3f3f3f3f)
        {
            cout << -1 << endl;
        }
        else
        {
            cout << f[a][b] << endl;
        }
    }
    return 0;
}